<?php
namespace Admin\Model;
use Think\Model;
/**
 * 查账管理
 *
 */

class PaysModel extends Model{
	protected $tableName = 'pay_record';

	const AUDIT_NEED = 0;/* 待审核 */
	const AUDIT_OK = 1;
	const AUDIT_ERR = 2; /* 审核失败 */

	const RECORD_STATUS_VALID = 1; //有效付款
	const RECORD_STATUS_INVALID = 2; //作废付款

	const PAY_UNKOWN = 0; /*未知*/
	const PYA_NEW = 1;  /*新单*/
	const PAY_XUFEI =2; /*续费*/
	const PAY_REFUND = 3; /*退款*/

	const XUFEI_BUKUAN = 0;/*补款*/
	const XUFEI_QIANXU = 1;/*前续*/
	const XUFEI_HOUXU = 2;/*后续*/

	const PAY_ONLINE = 0;//线上支付类型
	const PAY_OFFLINE = 20;//线下支付的订单



	/*添加新单查账*/
	public function add_new(){
		$uinfo = UserModel::loadFromSession();
		//验证用户唯一
		$r = array('d'=>NULL,'i'=>'','r'=>0);
 		if( !$this->creator ){//谁添加的查账,谁就是创建人
 			$this->creator = $uinfo['user_id'];
			$this->creator_role = $uinfo['role_id'];
			$this->creator_sector = $uinfo['sector_id'];
 		}
 		if (!$this->customer_sector) {
 			$this->customer_sector = $uinfo['sector_id'];
 		}
 		$this->create_time = date('Y-m-d H:i:s');
 		$this->audit_res = self::AUDIT_NEED;
 		$this->pay_type = self::PYA_NEW;
 		$this->pay_mount = doubleval( $this->pay_mount );
 		$this->is_export = 0;
 		$this->is_new = 1;

 		//参数验证
 		if( !$this->customer_name || !$this->customer_phone || !$this->customer_mount || !$this->customer_source ){
 			$r['i'] = "客户信息填写不完整";
 			return $r;
 		}
		if( !$this->customer_product || !$this->customer_price || !$this->contract_start_time || !$this->customer_buycount){
			$r['i'] = "产品订购信息不完整";
			return $r;
		}

		if( !$this->pay_name || !$this->pay_mount || !$this->pay_way || !$this->pay_time ){
			$r['i'] = "付款信息不完整";
			return $r;
		}

 		//客户唯一性验证
		$wheres = array();
		$wheres['customer_phone'] = array('eq', $this->customer_phone );
		$find = NULL;
		$find = $this->where( $wheres )->find();
		if( $find ){
			$r['i'] = "该电话已经被别人使用";
			$r['d'] = $find;
			return $r;
		}

		$MCustomer = M('Customer');
		$wheres = array();
		$wheres['phone'] = array('eq', $this->customer_phone);
		$find = $MCustomer->where( $wheres )->find();
		if( $find ){
			$r['i'] = "该电话号码已经被别人注册";
			$r['d'] = $find;
			return $r;
		}

		/*QQ号码唯一性验证*/
		if( $this->customer_qq ){
			$wheres = array();
			$wheres['customer_qq'] = array('eq', $this->customer_qq );
			$find = $this->where( $wheres )->find();
			if( $find ){
				$r['i'] = "该QQ号码已经被比人适用";
				$r['d'] = $find;
				return $r;
			}

			$wheres = array();
			$wheres['qq'] = array('eq', $this->customer_qq );
			$find = $MCustomer->where( $wheres )->find();
			if( $find ){
				$r['i'] = "该QQ号码已经被别人注册";
				$r['d'] = $find;
				return $r;
			}
		}

		/*微信ID唯一性验证*/
		if( $this->customer_weixin_id ){
			$wheres = array();
			$wheres['customer_weixin_id'] = array('eq', $this->customer_weixin_id );
			$find = $this->where( $wheres )->find();
			if( $find ){
				$r['i'] = "该微信ID已经被别人使用";
				$r['d'] = $find;
				return $r;
			}

			$wheres = array();
			$wheres['weixin_id'] = array('eq', $this->customer_weixin_id);
			$find = $MCustomer->where( $wheres )->find();
			if( $find ){
				$r['i'] = "该微信ID已经被别人注册";
				$r['d'] = $find;
				return $r;
			}
		}

		$did = $this->add();
		if( $did ){
			$r['i'] = "OK";
			$r['d'] = $did;
			$r['r'] = 1;
		}else{
			$r['i'] = "添加新单查账失败,ERROR:".$this->getError();
			$r['d'] = mysql_error();
		}

		return $r;
	}

	/*添加续费查账*/
	public function add_xufei(){
		$r = array('d'=>NULL,'i'=>'','r'=>0);
		$uinfo = UserModel::loadFromSession();
		if( !$this->creator ){
 			$this->creator = $uinfo['user_id'];
			$this->creator_role = $uinfo['role_id'];
			$this->creator_sector = $uinfo['sector_id'];
 		}
 		$this->customer_sales = $uinfo['user_id'];
 		$this->create_time = date('Y-m-d H:i:s');
 		$this->audit_res = self::AUDIT_NEED;
 		$this->pay_type = self::PAY_XUFEI;
 		$this->pay_mount = doubleval( $this->pay_mount );
 		$this->is_export = 0;
 		$this->is_new = 0;

 		if( !$this->customer_product || !$this->customer_price || !$this->contract_start_time || !$this->customer_buycount){
			$r['i'] = "产品订购信息不完整";
			return $r;
		}

		if( !$this->pay_name || !$this->pay_mount || !$this->pay_way || !$this->pay_time ){
			$r['i'] = "付款信息不完整";
			return $r;
		}

		$r['d'] = $this->add();

		if( $r['d'] ){
			$r['r'] = 1;
			$r['i'] = 'OK';
		}
		else{
			$r['i'] = '添加续费查账失败';
		}
		return $r;
	}

	/*删除查账信息*/
	public function del_pay(){

	}

	/*审核查账信息*/
	public function audit_pay(){
		//
	}


	// 添加退款信息
	public function save_fed($arr = array('customer_id'=>NULL,'pay_mount'=>NULL,'note'=>NULL),$format='Y-m-d H:i:s',$pay='payrecord_id'){
		$r = array('d'=>NULL,'i'=>'','r'=>0);
		if ( !$arr['customer_id'] || !$arr['pay_mount'] || !$arr['note'] ) {
			return $r;
		}

		$fedRes = M('customer')->join('contract ON contract.customer_id = customer.customer_id')
							   // ->join('LEFT JOIN product ON product.product_id = contract.product_id')
		                       ->where(array('customer.customer_id'=>$arr['customer_id']))->find();
		if ($fedRes) {
			$wheres = array();
			$wheres['pay_mount'] = '-'.$arr['pay_mount'];
			$wheres['note'] = $arr['note'];
			$wheres['customer_id'] = $arr['customer_id'];
			$wheres['pay_name'] =$fedRes['login_name'];
			$wheres['contract_id'] = $fedRes['contract_id'];
			$wheres['pay_type'] = 3;
			$wheres['is_export'] = 0;
			$wheres['create_time'] = date($format);
			$wheres['creator'] = $fedRes['sales_person'];
			$wheres['creator_sector'] = $fedRes['sales_sector'];
			$wheres['creator_role'] = $fedRes['sales_role'];
			$wheres['customer_sales'] = $fedRes['sales_person'];
			$wheres['customer_product'] = $fedRes['product_id'];
			$add = $this->add($wheres);
			if ($add) {
				$r['d'] = $this->where(array($pay => $add))->find();
				$r['r'] = 1;
			}else{
				$r['i'] = '添加失败';
			}
		}else{
			$r['i'] = '不存在合同信息';
		}
		return $r;

	}

	// 判定当前时间是否在指定时间之内
	public function judge_time($arr = array()){
		$r = array('d'=>NULL,'i'=>'','r'=>0);
		$now_time = strtotime(date('y-m-d h:i:s',time()));
		if ($now_time > $arr['start_time'] && $now_time < $arr['end_time']){
			$deTime = $now_time - $arr['start_time'];
			if ($deTime > 3600*24*30*3) {
				$r['r'] = 2;
			}else{
				$r['r'] = 1;
			}
		}
		return $r;
	}

	// 退款编辑
	public function edit_fed($arrEdit = array('customer_id'=>NULL,'pay_mount'=>NULL,'note'=>NULL,'payrecord_id'=>NULL)){
		$r = array('d'=>NULL,'msg'=>'','r'=>0);
		$pay_mount = str_replace('-','',$arrEdit['pay_mount']);
		if(!empty($arrEdit['customer_id']) || !empty($arrEdit['pay_mount'])){
			$customermsg = M('customer')->where(array('customer_id'=>$arrEdit['customer_id']))->find();
			$res = D('pay_record')->save(array('customer_id'=>$arrEdit['customer_id'],'pay_mount'=>'-'.$pay_mount,'note'=>$arrEdit['note'],'payrecord_id'=>$arrEdit['payrecord_id'],'customer_name'=>$customermsg['login_name'],'audit_res'=>0));
			if ($res) {
				$r['r'] = 1;
				$r['d'] = D('pay_record')->where(array('payrecord_id'=>$arrEdit['payrecord_id']))->find();
			}else
				$r['msg'] = '编辑失败';
		}

		 return $r;
	}

	public function general(){
		$Pproduct = M("product");
		//查询所属父类
		$uinfo = UserModel::loadFromSession();

    	if($uinfo['user_id'] =="1"){
    		$product_list = $Pproduct->field("product_id,name as product_name,sub_company")->select();
    		foreach ($product_list as &$value) {

    			$spwhere['sector_id'] =  $value['sub_company'];
    			$pname = M("sector")->field("sector_name")->where($spwhere)->find();
    			if(!empty($pname)){
    				$value['product_name'] = $pname['sector_name'] ." ".$value['product_name'] ;
    			}else{
    				$value['product_name'] = "总公司" ." ".$value['product_name'] ;
    			}
    		}
    	}else{
    		$Sector = M("sector");
    		$subtype['sector_id'] = $uinfo['sector_id'];
    		$type = $Sector->field("parent_company")->where($subtype)->find();

    		if($m_is_admin == true){
    			$wheretype['sub_company'] = $type['parent_company'];
    			$wheretype['product_type'] = array('between',array('-1','2'));
    		}else{
    			$wheretype['sub_company'] = $type['parent_company'];
    			$wheretype['product_type'] = 1;
    		}
    		$product_list = $Pproduct->field("product_id,name as product_name,sub_company")->where($wheretype)->select();
    	}

    	$PPrice = M("product_price");
    	//报价管理
    	foreach ($product_list as &$price_value) {
    		$p_price['product_id'] = $price_value['product_id'];
    		$price_value['price_list'] = $PPrice->field("price_id, name as price_name")->where($p_price)->select();
    	}
    	foreach ($product_list as $new_k => $new_v) {
    		if(!empty($new_v['price_list'])){
    			$new_price[] = $new_v;
    		}
    	}
    	$res['product_list'] = $new_price;
    	return $res;
	}

}
?>
